﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    static class GameDataTransfer
    {
        static public void AddGameData(GameDataWrapper gameDataWrapper)
        {
            ContainerNodeWrapper containerNodeWrapperParent = gameDataWrapper.NodeWrapperParent.ParentContainerNodeWrapper;
            var resourceName = containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;

            AddGameData(resourceName, scriptName, gameDataWrapper.NodeWrapperParent.Node, gameDataWrapper.GameData);
        }

        static public void AddGameData(string resourceName, string scriptName, NodeBase node, GameData gameData)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);
            var assemblyName = GameDataWrapper.GetGameToolType(gameData).Assembly.ManifestModule.FullyQualifiedName;
            var gameDataTypeName = GameDataWrapper.GetGameToolType(gameData).GetType().FullName;
            var gameDataIndex = node.GameDataCollection.IndexOf(gameData);

            DataTransferClient.Instance.TransferData((proxy) => proxy.AddGameData(gameData.GameItemName, gameData.GameToolName, resourceName, scriptName, containerNodeNameList, node.Name, assemblyName, gameDataTypeName));

            foreach (KeyValuePair<string, object> property in gameData.PropertyDictionary)
                AddProperty(resourceName, scriptName, node, gameDataIndex, property.Key, property.Value);
        }

        static public void RemoveGameData(string resourceName, string scriptName, NodeBase node, int index)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameData(resourceName, scriptName, containerNodeNameList, node.Name, index));
        }

        static public void AddProperty(GameDataWrapper gameDataWrapper, string propertyName, object propertyValue)
        {
            NodeBaseWrapper nodeWrapperParent = gameDataWrapper.NodeWrapperParent;
            ContainerNodeWrapper containerNodeWrapperParent = nodeWrapperParent.ParentContainerNodeWrapper;
            var resourceName = containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;
            var gameDataIndex = nodeWrapperParent.GameDataWrapperCollection.IndexOf(gameDataWrapper);

            AddProperty(resourceName, scriptName, nodeWrapperParent.Node, gameDataIndex, propertyName, propertyValue);
        }

        static public void AddProperty(string resourceName, string scriptName, NodeBase node, int gameDataIndex, string propertyName, object propertyValue)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);
            var propertyStringValue = GameEngine.GetStringRepresention(propertyValue);
            var assemblyName = propertyValue.GetType().Assembly.ManifestModule.FullyQualifiedName;
            var propertyTypeName = propertyValue.GetType().FullName;

            DataTransferClient.Instance.TransferData((proxy) => 
                proxy.AddGameDataProperty(resourceName, scriptName, containerNodeNameList, node.Name, gameDataIndex, propertyName, propertyStringValue, assemblyName, propertyTypeName));
        }

        static public void RemoveProperty(GameDataWrapper gameDataWrapper, string propertyName)
        {
            NodeBaseWrapper nodeWrapperParent = gameDataWrapper.NodeWrapperParent;
            ContainerNodeWrapper containerNodeWrapperParent = nodeWrapperParent.ParentContainerNodeWrapper;
            var resourceName = containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;

            RemoveProperty(resourceName, scriptName, nodeWrapperParent.Node, gameDataWrapper.GameData, propertyName);
        }

        static public void RemoveProperty(string resourceName, string scriptName, NodeBase node, GameData gameData, string propertyName)
        {
            var gameDataIndex = node.GameDataCollection.IndexOf(gameData);
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameDataProperty(resourceName, scriptName, containerNodeNameList, node.Name, gameDataIndex, propertyName));
        }

        //static public void RemoveProperty(string resourceName, string scriptName, NodeBase node, string propertyName, int gameDataIndex)
        //{
        //    var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);

        //    DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameDataProperty(resourceName, scriptName, containerNodeNameList, node.Name, gameDataIndex, propertyName));
        //}

        static public void Update(GameDataWrapper gameDataWrapper, string propertyName, object propertyValue)
        {
            NodeBaseWrapper nodeWrapperParent = gameDataWrapper.NodeWrapperParent;
            ContainerNodeWrapper containerNodeWrapperParent = nodeWrapperParent.ParentContainerNodeWrapper;
            var resourceName = containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;
            var gameDataIndex = nodeWrapperParent.GameDataWrapperCollection.IndexOf(gameDataWrapper);

            Update(resourceName, scriptName, nodeWrapperParent.Node, gameDataIndex, propertyName, propertyValue);
        }

        static public void Update(string resourceName, string scriptName, NodeBase node, int gameDataIndex, string propertyName, object propertyValue)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);
            var propertyStringValue = GameEngine.GetStringRepresention(propertyValue);
            var assemblyName = propertyValue.GetType().Assembly.ManifestModule.FullyQualifiedName;
            var propertyTypeName = propertyValue.GetType().FullName;

            DataTransferClient.Instance.TransferData((proxy) =>
                proxy.UpdateGameData(resourceName, scriptName, containerNodeNameList, node.Name, gameDataIndex, propertyName, propertyStringValue, assemblyName, propertyTypeName));
        }

        static public void UpdateGameItemName(GameDataWrapper gameDataWrapper)
        {
            NodeBaseWrapper nodeWrapperParent = gameDataWrapper.NodeWrapperParent;
            ContainerNodeWrapper containerNodeWrapperParent = nodeWrapperParent.ParentContainerNodeWrapper;
            var resourceName = containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;
            var gameDataIndex = nodeWrapperParent.GameDataWrapperCollection.IndexOf(gameDataWrapper);

            UpdateGameItemName(gameDataWrapper.GameData.GameItemName, resourceName, scriptName, nodeWrapperParent.Node, gameDataIndex);
        }

        static public void UpdateGameItemName(string gameItemName, string resourceName, string scriptName, NodeBase node, int gameDataIndex)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateGameDataGameItemName(gameItemName, resourceName, scriptName, containerNodeNameList, node.Name, gameDataIndex));
        }
    }
}
